package com.shoulder.resourceserver.handler;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.HashMap;
import java.util.Map;

/**
 * 扩展密码加密方式
 */
public class PasswordEncoderHandler {
    public static final String encodingId = "bcrypt";

    private PasswordEncoderHandler() {
    }

    public static PasswordEncoder createDelegatingPasswordEncoder() {
        Map<String, PasswordEncoder> encoders = initEncoders();
        DelegatingPasswordEncoder delegatingPasswordEncoder = new DelegatingPasswordEncoder("bcrypt", encoders);
        delegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(encoders.get(encodingId));
        return delegatingPasswordEncoder;
    }

    public static Map<String, PasswordEncoder> initEncoders() {

        Map<String, PasswordEncoder> encoders = new HashMap<>();
        encoders.put(encodingId, new BCryptPasswordEncoder());
        //todo 在这里自行扩展自己系统密码实现，spring security 已支持的其他方式在该版本中已被标记为弃用 因此不推荐使用
        return encoders;
    }

}
